package com.spring.boot.standard.admin.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.spring.boot.standard.admin.entity.Developerconfiguration;
import com.spring.boot.standard.admin.entity.Versions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import com.spring.boot.standard.admin.common.utils.GsonUtil;
import com.spring.boot.standard.admin.dao.TemplatesRepository;
import com.spring.boot.standard.admin.entity.Templates;
import com.spring.boot.standard.admin.service.TemplatesService;

/**
 * @author qinhao
 * @since Thu Sep 09 14:31:17 CST 2021
 */
@Service
@EnableTransactionManagement
public class TemplatesServiceImpl implements TemplatesService {
	
    @Autowired
    private TemplatesRepository templatesRepository;

	@PersistenceContext
	EntityManager entityManager;

    @SuppressWarnings("serial")
	@Override
	public Page<Templates> findAll(Pageable pageable,Object[] args) {
		Page<Templates> result = templatesRepository.findAll(new Specification<Templates>() {
	        @Override
	        public Predicate toPredicate(Root<Templates> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				//List<Predicate> list = new ArrayList<Predicate>(); 多条件搜索的写法
				if (!StringUtils.isEmpty(args[0]) && !StringUtils.isEmpty(args[1])) {
					/**多条件搜索的写法
					list.add(cb.like(root.get("templateid").as(String.class), "%" + args[0] + "%"));
					list.add(cb.like(root.get("templatename").as(String.class), "%" + args[0] + "%"));
					list.add(cb.like(root.get("packagename").as(String.class), "%" + args[0] + "%"));
					list.add(cb.like(root.get("projectname").as(String.class), "%" + args[0] + "%"));
					list.add(cb.like(root.get("relativepath").as(String.class), "%" + args[0] + "%"));
					list.add(cb.like(root.get("newfilename").as(String.class), "%" + args[0] + "%"));
					list.add(cb.like(root.get("templatecontent").as(String.class), "%" + args[0] + "%"));
					list.add(cb.like(root.get("remark").as(String.class), "%" + args[0] + "%"));
					Predicate[] p = new Predicate[list.size()];
					return cb.or(list.toArray(p));*/
					return cb.like(root.get(args[0].toString()).as(String.class),"%" + args[1] + "%");
				}else{
					return null;
				}
	        }
	    }, pageable);
	    return result;
	}

	@Override
	public List<Templates> findList(String groupname){
		Templates templates = new Templates();
		templates.setGroupname(groupname);
		Example<Templates> example = Example.of(templates);
		try {
			return templatesRepository.findAll(example);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public List<Templates> findListSimple(String groupname){
		return templatesRepository.findListByGroupname(groupname);
	}

	@Override
	public Templates findById(String templateid) {
		return templatesRepository.findById(templateid).get();
	}

	@Override
	public void deleteById(String templateid) {
		templatesRepository.deleteById(templateid);;
	}

	@Override
	public Templates save(Templates templates) {
		return templatesRepository.save(templates);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int saveFromList(List<Map<String,String>> list) throws Exception {
		int i=0;
		List<Templates> templateslist = new ArrayList<Templates>();
		for(Map<String,String> map:list){
			Templates templates = GsonUtil.getGson().fromJson(GsonUtil.getGson().toJson(map),Templates.class);
			templateslist.add(templates);
		}
		for(Templates templates:templateslist){
			templatesRepository.save(templates);
			i++;
		}
		return i;
	}

    
}
